using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-入库商品详细服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpInstockProductService : IApplicationService 
{
    private readonly SugarRepository<ErpInstockProduct> _thisRepository;
    private readonly SugarRepository<ErpProduct> _productRepository;
    public ErpInstockProductService(SugarRepository<ErpInstockProduct> thisRepository
    ,SugarRepository<ErpProduct> productRepository)
    {
        _thisRepository = thisRepository;
        _productRepository = productRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpInstockProductInfoDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Id!=0,m=>m.InStockId==param.Id)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        var list = new List<ErpInstockProductInfoDto>();
        if (query.Items.Count>0)
        {
            var productIds = query.Items.Select(m => m.ProductId).ToList();
            var productList = await _productRepository.AsQueryable()
                .Where(m => productIds.Contains(m.Id))
                .Includes(m=>m.Type)
                .Includes(m=>m.Unit)
                .ToListAsync();
            list = productList.Adapt<List<ErpInstockProductInfoDto>>();
            foreach (var item
                     in list)
            {
                var productModel = query.Items.FirstOrDefault(m => m.ProductId == item.Id);
                if (productModel==null) continue;
                item.Count = productModel.Count;
            }
        }
        return new PageResult<ErpInstockProductInfoDto>()
        {
            TotalItems = query.TotalItems,
            TotalPages = query.TotalPages,
            Items = list
        };
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpInstockProductDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<ErpInstockProductDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(ErpInstockProductDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<ErpInstockProduct>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(ErpInstockProductDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<ErpInstockProduct>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
